package com.yin.myproject.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;

/**
 * Created by Eason on 2017/10/10 0010.
 */
public class HdfsDemo {
    public static void main(String[] args) throws IOException {
        String uri = "hdfs://172.16.20.123:9000/";
//        String uri = "hdfs://192.168.122.128:9000/";
        Configuration config = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), config);
        // 列出hdfs上/user/fkong/目录下的所有文件和目录
        FileStatus[] statuses = fs.listStatus(new Path("/user/hdfs"));
        System.out.println("***********************************");
        for (FileStatus status : statuses) {
            System.out.println(status);
        }

        boolean mkdir = HdfsDemo.mkdir("/user/test", fs);

    }

    /**
     * 创建目录
     *
     * @param path
     * @param fs
     * @return
     * @throws IOException
     */
    public static boolean mkdir(String path, FileSystem fs) throws IOException {
        Path srcPath = new Path(path);
        return fs.mkdirs(srcPath);
    }

    /**
     * 创建新文件
     *
     * @param path
     * @param fs
     * @throws IOException
     */
    public static void createFile(String path, FileSystem fs) throws IOException {
        Path dstPath = new Path(path); //目标路径
        //打开一个输出流
        FSDataOutputStream outputStream = fs.create(dstPath);
        outputStream.write("this is test log content".getBytes());
        outputStream.close();
        fs.close();
    }


    /**
     * 上传本地文件
     *
     * @param src
     * @param dest
     * @param fs
     * @throws IOException
     */
    public static void uploadFile(String src, String dest, FileSystem fs) throws IOException {
        Path srcPath = new Path(src); //原路径
        Path dstPath = new Path(dest); //目标路径
        //调用文件系统的文件复制函数,前面参数是指是否删除原文件，true为删除，默认为false
        fs.copyFromLocalFile(false, srcPath, dstPath);
        fs.close();
    }

}
